package com.aaa.sb.controller;

import com.aaa.sb.service.NewsService;
import com.aaa.sb.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * className:NewsController
 * discription:
 * author:luRuiHua
 * createTime:2018-11-22 09:48
 */
@Controller
@RequestMapping("/news")
@SuppressWarnings("all")
public class NewsController {
    //依赖注入
    @Autowired
    private NewsService newsService;
    //取出配置文件（application.properties）中的值，赋给uploadPath类变量
    @Value("${upload.path}")
    private String uploadPath;

    private final ResourceLoader resourceLoader;
    @Autowired
    public NewsController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    /**
     * 跳转添加<tr><td></td><td><input type="text" name="title"></td></tr>
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        return "add";
    }
    /**
     * 跳转更改<tr><td></td><td><input type="text" name="title"></td></tr>
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(@RequestParam Map paramMap,Model model) {
        List<Map> news = newsService.selectNewsMsgById(paramMap);
        model.addAttribute("newsList",news);
        return "update";
    }

    /**
     * 新闻添加
     * @return
     */
    @RequestMapping("/add")
    public String add(@RequestParam Map map, @RequestParam MultipartFile pic) {
        if(pic != null) {
            String newFilePath = FileUtil.uploadFile(uploadPath, pic);
            //将文件存放到map中
            map.put("picPath",newFilePath);
            //将源文件名存到数据库
            map.put("fileName",pic.getOriginalFilename());
        }
        newsService.add(map);
        return "redirect:list";
    }

    /**
     * 新闻列表
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model){
        model.addAttribute("newsList",newsService.getList());
        return "list";
    }

    /**
     * 新闻更改
     * @return
     */
    @RequestMapping("/updateOne")
    public String updateOne(@RequestParam Map map, @RequestParam MultipartFile pic) {
        if(pic != null) {
            String newFilePath = FileUtil.uploadFile(uploadPath, pic);
            //将文件存放到map中
            map.put("picPath",newFilePath);
            //将源文件名存到数据库
            map.put("fileName",pic.getOriginalFilename());
        }
        System.out.println("map:"+map);
        newsService.updateOne(map);
        return "redirect:list";
    }
    /**
     * 新闻删除
     * @return
     */
    @RequestMapping("/toDelete")
    public String toDelete(@RequestParam Map map) {
        newsService.toDelete(map);
        return "redirect:list";
    }

    /**
     * 显示本地图片，需要上面一个依赖注入resourceLoader
     * @param fileName
     * @return
     */
    @RequestMapping("/show")
    public ResponseEntity show(String fileName){
        try {
            // 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
            return ResponseEntity.ok(resourceLoader.getResource("file:" + uploadPath + fileName));
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 文件下载
     * @param fileName
     * @param response
     */
    @RequestMapping("/downLoad")
    public void downLoadFile(String fileName, HttpServletResponse response){
        FileUtil.downLoad(fileName,response);
    }
}
